<?php

ini_set("display_errors", 0);

error_reporting(E_ALL ^ E_NOTICE);

error_reporting(E_ALL ^ E_WARNING);



header("Content-type: text/html; charset=utf-8");

$time = time();


$servername = 'localhost';
$username = 'ltc_tophp_cn';  //用户名
$password = 'pHGEXsQM7DCEFmW8';  //密码
$dbname = "ltc_tophp_cn";   //数据库
$conn=mysql_connect($servername, $username, $password) or die('连接失败');
mysql_select_db($dbname);
mysql_query('set names utf8');

//当前业绩大比拼期数
$now_yj_time = mysql_fetch_array(mysql_query("SELECT value FROM erj_sys WHERE  name = 'now_yj_time'"));

$now_yj_time = $now_yj_time['value'];

//周期
$yj_countdown = mysql_fetch_array(mysql_query("SELECT value FROM erj_sys WHERE  name = 'yj_countdown'"));

$yj_countdown = $yj_countdown['value'];
$yj_countdown = 60*60*24*$yj_countdown; //转换为时间戳

//结束时间
$end_yj_time = $now_yj_time+$yj_countdown-1;


if($time >= $end_yj_time){

    //奖池金额
    $list_pond = mysql_fetch_array(mysql_query("SELECT value FROM erj_sys WHERE  name = 'list'"));

    $list_pond = $list_pond['value'];

    //前十名奖励所占比列
if($list_pond == 0){

    die("本轮无业绩");

}
    $rank_deuce = mysql_fetch_array(mysql_query("SELECT value FROM erj_sys WHERE  name = 'rank_deuce'"));

    $rank_deuce = $rank_deuce['value'];



    $pond2 = $list_pond * $rank_deuce/100; //前十奖励总额
    $pond1 = $list_pond - $pond2; //平分总额


    //满足条件的用户
   $res = mysql_query("SELECT * FROM erj_user");

    while($user = mysql_fetch_array($res)){

        $userlist[] = $user;

    }

    //所需推荐人数
    $rank_num = mysql_fetch_array(mysql_query("SELECT value FROM erj_sys WHERE  name = 'rank_num'"));

    $rank_num = $rank_num['value'];
    //前N名获得奖励
    $rank = mysql_fetch_array(mysql_query("SELECT value FROM erj_sys WHERE  name = 'ranks'"));

    $rank = $rank['value'];

    //初始化最终奖励用户
    $list = array();





    foreach ($userlist as $k => $v) {

        if ($v['round_child_num'] >= $rank_num) {
            $isseat = mysql_fetch_array(mysql_query("SELECT id FROM erj_seatlist WHERE  user_id = ".$v['id']));

            $isseat = $isseat['id'];
            if ($isseat > 0) {  //必须有过捐赠记录
                $user_name = mysql_fetch_array(mysql_query("SELECT username FROM erj_user WHERE  id = ".$v['id']));
                $user_name = $user_name['username'];
                $user_name = change_username($user_name);
                $list[] = array('uid' => $v['id'],'user' => $user_name, 'num' => $v['round_child_num'], 'time' => $v['activate_time']);
            }
        }
    }

    //按推荐人数、占位时间进行排名
    array_multisort(array_column($list, 'num'), SORT_DESC, array_column($list, 'time'), SORT_ASC, $list);

    //平分奖励用户
    $list1 = array_slice($list, 0, $rank);
    //前十名
    $list2 = array_slice($list, 0, 10);

    $num1 = count($list1);
    $num2 = count($list2);

    $pond1 =  round($pond1/$num1,8);
    $pond2 =  round($pond2/$num2,8);


    //所有满足条件的用户平分奖励
    foreach($list1 as $k => $v){

        $uid = $v['uid'];
        $now_usermoney = mysql_fetch_array(mysql_query("SELECT usermoney FROM erj_user WHERE  id = '$uid'"));



        $new_usermoney = $now_usermoney['usermoney'] + $pond1;

     

        mysql_query("UPDATE erj_user SET usermoney = $new_usermoney WHERE id = '$uid'");

        $user = $uid;

        $money = $pond1;

        $type = 9;

        $msg = $now_yj_time.'期，业绩大比拼结算奖励，获得'.$pond1;
        //插入记录


        $ins = "INSERT INTO erj_log (user_id,money,amount,type , msg, date) VALUES ($user,$money,0,$type,'$msg',$time)";

        mysql_query($ins);

    }

    //前10名用户平分前10奖励
    foreach($list2 as $k => $v){

        $uid = $v['uid'];
        $now_usermoney = mysql_fetch_array(mysql_query("SELECT usermoney FROM erj_user WHERE  id = '$uid'"));

        $new_usermoney = $now_usermoney['usermoney'] + $pond2;

        mysql_query("UPDATE erj_user SET usermoney = $new_usermoney WHERE id = '$uid'");
        $user = $uid;

        $money = $pond2;

        $type = 9;

        $msg =$now_yj_time.'期，业绩大比拼前10名奖励，获得'.$pond2;

        //插入记录

        $ins = "INSERT INTO erj_log (user_id,money,amount,type ,msg, date) VALUES ($user,$money,0,$type,'$msg',$time)";

        mysql_query($ins);

    }



    foreach($userlist as $k => $v){
        mysql_query("UPDATE erj_user SET round_child_num = 0 WHERE id = ".$v['id']);

    }


    //重置奖池
    $yj_time = time();

    $yj_pond['value'] = 0;

    mysql_query("UPDATE erj_sys SET value = $yj_time WHERE name = 'now_yj_time'");
    mysql_query("UPDATE erj_sys SET value = 0 WHERE name = 'list'");


    die("业绩大比拼已结算！");

}else{
    die("未到结算时间！");
}



function change_username($user_name){

    $array = explode('@', $user_name);

    $username = $array[0];

    if(strlen($username)>3){

        $first = substr($username, 0, 2);

        $last = substr($username, -2);

        $username = $first."***".$last."@".$array[1];

    }else{

        $username = $user_name;

    }

    return $username;

}



?>